package gui.swings;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.border.TitledBorder;

public class SwingButtonGroup extends JPanel {

	static String[] ids = { "JUNE", "WARD", "BEAVER" };
	
	static JPanel makeBPanel(Class bClass,String[] ids){
		ButtonGroup bg=new ButtonGroup();
		JPanel jp=new JPanel();
		String title=bClass.getName();
		title=title.substring(title.lastIndexOf(".")+1);
		jp.setBorder(new TitledBorder(title));
		for(int i=0;i<ids.length;i++){
			AbstractButton ab=new JButton("Failed");
			
			try {
				Constructor ctor=bClass.getConstructor(new Class[]{String.class});
				try {
					ab=(AbstractButton) ctor.newInstance(new Object[]{ids[i]});
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			}
			 bg.add(ab);
			 jp.add(ab);
			
		}
		
		return jp;
	}
	
	public SwingButtonGroup(){
		this.add(makeBPanel(JButton.class,ids));
		this.add(makeBPanel(JToggleButton.class,ids));
		this.add(makeBPanel(JCheckBox.class,ids));
		this.add(makeBPanel(JRadioButton.class,ids));
	}
	
	public static void main(String[]args){
		SwingShow.inFrame(new SwingButtonGroup(), 500, 500);
	}
	
}
